package com.shz.controller.helper;

import com.shz.controller.AliPayController;
import com.shz.utils.SnowflakeIdGenerator;
import com.shz.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
@Api(tags = "支付状态模块")
@RestController
public class HelperController {
    private static final ConcurrentHashMap<Integer, Long> userMap = new ConcurrentHashMap<>();
    @ApiOperation(value = "支付页面获取全局唯一id后id作为订单进行下单")
    @GetMapping("/helper/getGlobalId")
    public static Long getGlobalId(){
        Integer uid = UserUtil.getCurrentUser().getId();
        if(userMap.containsKey(uid)){
            return userMap.get(uid);
        }
        long globalId = SnowflakeIdGenerator.getGlobalId();
        userMap.put(uid,globalId);
        return globalId;
    }
    public static void remove (int uid){
         userMap.remove(uid);
    }
    @ApiOperation(value = "判断是否有未支付订单（判断是否出现弹窗,显示内容是是否取消未支付订单前往该行程预约）" +
            "确认执行【删除未支付订单】接口,获取全局id之前一定要判断")
    @GetMapping("/isHaveThisOrder")
    public static boolean isHaveThisOrder(){

        Integer uid = UserUtil.getCurrentUser().getId();
        return userMap.containsKey(uid);
    }
    @ApiOperation(value = "删除未支付订单")
    @DeleteMapping("/deleteHaveOrder")
    public static boolean deleteHaveOrder(){
        Integer uid = UserUtil.getCurrentUser().getId();
        if(userMap.containsKey(uid)){
            AliPayController.deleteHaveOrder(userMap.get(uid));
        }
        remove(uid);
        return true;
    }



}
